package com.seventh.restaurant.web;

import com.seventh.restaurant.domain.Table;
import com.seventh.restaurant.service.TableService;
import com.seventh.restaurant.web.util.HeaderUtil;
import com.seventh.restaurant.web.util.PaginationUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import static org.hibernate.id.IdentifierGenerator.ENTITY_NAME;

@RestController
@RequestMapping("/api")
public class TableResource {

    private final TableService tableService;


    public TableResource(TableService tableService) {
        this.tableService = tableService;
    }


    @ApiOperation(value="新增餐桌信息")
    @PostMapping("/table")
    public ResponseEntity<Table> createTable(@RequestParam String tableNo,@RequestParam String size) throws Exception {
        if (tableNo!=null && size!=null){
            Table table = new Table();
            table.setTableNo(tableNo);
            table.setSize(size);
            Table table1 = tableService.insertTable(table);
            return ResponseEntity.ok().body(table1);
        }else {
            return new ResponseEntity<Table>(HttpStatus.BAD_REQUEST);
        }
    }


    @ApiOperation(value="删除餐桌信息")
    @DeleteMapping("/table")
    public ResponseEntity<Table> deleteTable(@RequestParam Long id) throws Exception {
        if (id!=null){
            tableService.deleteTable(id);
            return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("Table", id.toString())).build();
        }else {
            return new ResponseEntity<Table>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="修改餐桌信息")
    @PutMapping("/table")
    public ResponseEntity<Table> updateTable(@RequestParam String tableNo,@RequestParam String size,@RequestParam String isrecord,@RequestParam String isbusy) throws Exception {
        if (tableNo!=null && size!=null && isrecord!=null && isbusy!=null){
            Table table1 = new Table();
            table1.setTableNo(tableNo);
            table1.setSize(size);
            table1.setIsRecord(isrecord);
            table1.setIsBusy(isbusy);
            Table table = tableService.updateTable(table1);
            return ResponseEntity.ok()
                    .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, table.getId().toString()))
                    .body(table);
        }else {
            return new ResponseEntity<Table>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="获得可用餐桌信息列表List")
    @GetMapping("/table/validlist")
    public ResponseEntity<List<Table>> getValidTables() {
        List<Table> tables = tableService.findAllValidTables();
        return new ResponseEntity<>(tables, HttpStatus.OK);
    }

    @ApiOperation(value="获得所有餐桌信息列表List")
    @GetMapping("/table/list")
    public ResponseEntity<List<Table>> getAllTables() {
        List<Table> tables = tableService.findAllTables();
        return new ResponseEntity<>(tables, HttpStatus.OK);
    }

    @ApiOperation(value="根据餐桌编号获得餐桌信息")
    @GetMapping("/table/one")
    public ResponseEntity<Table> getOneTable(@RequestParam String tableNo) {
        if (tableNo!=null){
            Table table = tableService.findTable(tableNo);
            return new ResponseEntity<>(table, HttpStatus.OK);
        }else {
            return new ResponseEntity<Table>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="根据用餐人数获得可用的餐桌信息")
    @GetMapping("/table/valid")
    public ResponseEntity<List<Table>> getValidTables(@RequestParam String peoplenum) {
       List<Table> tables = tableService.findValidTables(peoplenum);
       return new ResponseEntity<>(tables, HttpStatus.OK);

    }

}
